/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.activity.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.uml2.uml.ControlFlow;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.MergeNode;
import org.eclipse.uml2.uml.NamedElement;

import com.cea.papyrus.core.PapyrusMessages;
import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editor.mouseoveraction.ActionHandleManager;
import com.cea.papyrus.core.editpart.GraphNodeWithSemanticEditPart;
import com.cea.papyrus.core.figures.IconFigure;
import com.cea.papyrus.core.ui.dialogs.PapyrusMessageDialog;
import com.cea.papyrus.diagram.activity.figure.DecisionMergeNodeFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

public class MergeNodeEditPart extends GraphNodeWithSemanticEditPart {
	

	/**
	 * The constructor.
	 * 
	 * @param modelManager the associated ModelManager
	 * @param graphNode the GraphNode to be managed
	 */
	public MergeNodeEditPart(GraphNode graphNode, ModelManager modelManager){
		super(graphNode, modelManager);
	}
 
	 /* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.GraphNodeWithSemanticEditPart#createFigure()
	 */
	@Override
	protected IFigure createFigure(){
		// Try to get figure from ContainerWithSemanticEditPart
		IFigure fig = super.createFigure();
		if (!(fig instanceof IconFigure)) {
			return new DecisionMergeNodeFigure() ;		
		}
		// Else
		return fig;	
	}

	 /* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#refreshVisuals()
	 */
	@Override
	protected void refreshVisuals() {
		Dimension size = getGraphNode().getSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p, size);
		
		IFigure fig = getFigure();
		
		
		// Classifier
		if (fig instanceof IconFigure) {
			IconFigure icf = (IconFigure) getFigure();
			icf.setName(((NamedElement) getUmlElement()).getName());
		} else {
			MergeNode currentNode = getUmlElement();
			com.cea.papyrus.umlutils.Element currentElement = new com.cea.papyrus.umlutils.Element(getUmlElement());
			((DecisionMergeNodeFigure) fig).refreshColors(getGraphNode());
			((DecisionMergeNodeFigure) fig).refreshPresentation(getPresentation(),currentNode, getGraphNode());
			
			boolean hasIcon = currentElement.hasIcon();
			boolean hasShape = currentElement.hasShape();
		
			((DecisionMergeNodeFigure) fig).refreshStereotypes(((DecisionMergeNodeFigure) fig).stereotypesLabel(this), getPresentation(), hasIcon, hasShape);
		}  

		((GraphicalEditPart) getParent()).setLayoutConstraint(this, fig, r);
	}

	/**
	 * Install Action handles for this editPart. Use same toolEntries than in
	 * palette
	 */
	protected void installActionHandle() {
		// Install mouse hover
		ActionHandleManager actionMngr = new ActionHandleManager(this);
		// Create a toolbar of actions.
		try {
			PaletteDrawer entries = new PaletteDrawer("creation tool");
			// Add shortcuts on actor figure for creating a association with an action
			entries.add(PapyrusPlugin.creationToolFactory
					.getConnectionCreationToolEntry(
							PapyrusMessages.AddControlFlowCommand_Label,
							PapyrusMessages.AddControlFlowCommand_Description,
							ControlFlow.class, null));
			actionMngr.addTool(entries, PositionConstants.EAST, false, 5);
		} catch (Exception e) {
			PapyrusMessageDialog.display(e.getMessage(), e);
		}
	}
	
	 /* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.GraphNodeWithSemanticEditPart#getUmlElement()
	 */
	@Override
	public MergeNode getUmlElement(){
		Element e = ((Uml1SemanticModelBridge)getGraphNode().getSemanticModel()).getElement();
        // Case of element import
        if(e instanceof ElementImport)
        	return (MergeNode)((ElementImport)e).getImportedElement();
		return (MergeNode)e;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.GraphNodeWithSemanticEditPart#createEditPolicies()
	 */
	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		installActionHandle();
	}

	
}
